home *** CD-ROM | disk | FTP | other *** search
/ NeXT Education Software Sampler 1992 Fall / NeXT Education Software Sampler 1992 Fall.iso / Programming / Source / HippoDraw / hippo / hippoxdr.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-04-28  |  12.9 KB  |  605 lines

  1. /*
  2.  * hippoxdr.c - XDR routines for hippo I/O
  3.  *
  4.  * Copyright (C)  1991  The Board of Trustees of The Leland Stanford
  5.  * Junior University.  All Rights Reserved.
  6.  *
  7.  * $Id: hippoxdr.c,v 3.14 1992/04/07 19:02:44 rensing Rel $
  8.  *
  9.  *  by Paul Rensing
  10.  */
  11.  
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <limits.h>
  15. #include "hippo.h"
  16.  
  17. #ifdef VM
  18. #include <manifest.h>
  19. #include "h_xdr.h"
  20. #include "h_util.h"
  21. #else
  22. #include "hippoxdr.h"
  23. #include "hippoutil.h"
  24. #endif
  25.  
  26. /*
  27.  * for machines which are naturally big-endian, IEEE floating point,
  28.  * define EASY_XDR so that the ntuple data array (which is the vast majority
  29.  * of a hippo file) is written out without checking for conversion.
  30.  */
  31. #if defined(aix6000) || defined(NeXT) || defined(sun4) || defined(sgi)
  32. #define EASY_XDR
  33. #endif
  34.  
  35. GLOB_QUAL const char hippoxdr_c_rcsid[] = "$Id: hippoxdr.c,v 3.14 1992/04/07 19:02:44 rensing Rel $";
  36.  
  37. GLOB_QUAL const char hippostruct_h_rcsid[] = STRUCT_VERSION;
  38. #define CURRENT_VER_NUM 3.8
  39.     /* version number of data being read/written */
  40. static float fileVersion = CURRENT_VER_NUM;
  41.  
  42. static bool_t xdr_mywrapstring(XDR *xdrs, char **string);
  43.  
  44.  
  45. static bool_t xdr_mywrapstring(XDR *xdrs, char **string)
  46. {
  47. #ifndef __STDC__
  48.      static
  49. #endif
  50.      char blank[]=" ";
  51.      char *pb = blank;
  52.      int rc;
  53.      
  54.      if (xdrs->x_op == XDR_ENCODE && *string == NULL)
  55.       rc = xdr_wrapstring(xdrs,&pb);
  56.      else
  57.       rc = xdr_wrapstring(xdrs,string);
  58.      
  59.      return rc;
  60. }
  61.  
  62.  
  63. bool_t xdr_floatarray(XDR *xdrs, caddr_t *objp, unsigned int *count,
  64.               unsigned int max)
  65. {
  66. #ifdef EASY_XDR
  67.      switch (xdrs->x_op)
  68.      {
  69.      case XDR_FREE:
  70.       return xdr_array(xdrs, objp, count, max, sizeof(float), xdr_float);
  71.       break;
  72.       
  73.      case XDR_DECODE:        /* ie. read */
  74.       if (! XDR_GETLONG(xdrs,(long *)count)) 
  75.            return (FALSE);
  76.       
  77.       if ( *objp == NULL && *count != 0 &&
  78.           (*objp = (char *) malloc(*count*sizeof(float))) == NULL)
  79.            return FALSE;
  80.       
  81.       if (*count != 0)
  82.            return XDR_GETBYTES(xdrs,*objp,
  83.                    (unsigned int) *count*sizeof(float));
  84.       break;
  85.       
  86.      case XDR_ENCODE:
  87.       if (! XDR_PUTLONG(xdrs,(long *)count)) 
  88.            return (FALSE);
  89.  
  90.       if (*count > 0)
  91.            return XDR_PUTBYTES(xdrs,*objp,*count*sizeof(float));
  92.       break;
  93.      }      
  94.      return 1;
  95. #else
  96.      return xdr_array(xdrs, objp, count, max, sizeof(float), xdr_float);
  97. #endif
  98. }
  99.  
  100. bool_t xdr_rectangle(XDR *xdrs, rectangle *objp)
  101. {
  102.      if (!xdr_float(xdrs, &objp->origin.x)) {
  103.       return (FALSE);
  104.      }
  105.      if (!xdr_float(xdrs, &objp->origin.y)) {
  106.       return (FALSE);
  107.      }
  108.      if (!xdr_float(xdrs, &objp->size.width)) {
  109.       return (FALSE);
  110.      }
  111.      if (!xdr_float(xdrs, &objp->size.height)) {
  112.       return (FALSE);
  113.      }
  114.      return (TRUE);
  115. }
  116.  
  117.  
  118.  
  119.  
  120. bool_t xdr_func_id(XDR *xdrs, func_id *objp)
  121. {
  122.      func_id *next=NULL;
  123.      bool_t more_data;
  124.      
  125.      while (1)
  126.      {
  127.       more_data = (*objp != NULL);
  128.       if (!xdr_bool(xdrs, &more_data))
  129.       {
  130.            return (FALSE);
  131.       }
  132.       
  133.       if (!more_data) break;
  134.       
  135.       if (xdrs->x_op == XDR_FREE)
  136.            next = &(*objp)->next;
  137.       
  138.       if (!xdr_reference(xdrs, (caddr_t *)objp, 
  139.                  sizeof(func_id_t), xdr_func_id_t)) {
  140.            return (FALSE);
  141.       }
  142.       objp = (xdrs->x_op == XDR_FREE) ?
  143.            next : &(*objp)->next;
  144.      }
  145.      *objp = NULL;
  146.      
  147.      return(TRUE);
  148. }                
  149.  
  150. bool_t xdr_func_id_t(XDR *xdrs, func_id_t *objp)
  151. {
  152.      char *tmp;
  153.  
  154.      /*
  155.       * bug on RS/6000: &objp->name is not allowed because it is not
  156.       * in memory
  157.       */
  158.      tmp = objp->name;
  159.      
  160.      if (!xdr_string(xdrs, &tmp, FUNCNAMELEN)) {
  161.       return (FALSE);
  162.      }
  163.      
  164.      /*
  165.       * don't bother with the function pointer. Reconstruct it later
  166.       * using the function name (objp->name).
  167.       */
  168.      
  169.      if (!xdr_array(xdrs, (char **)&objp->paramBlk, 
  170.             (u_int *)&objp->blkSize, UINT_MAX, 
  171.             sizeof(double), xdr_double)) {
  172.       return (FALSE);
  173.      }
  174.      
  175.      if (!xdr_enum(xdrs, (int *)&objp->lineStyle)) {
  176.       return (FALSE);
  177.      }
  178.      
  179.      return (TRUE);
  180. }
  181.  
  182.  
  183. bool_t xdr_ntuple_t(XDR *xdrs, ntuple_t *objp)
  184. {
  185.      unsigned int i;
  186.      
  187.      if (!xdr_int(xdrs, &objp->ndim))
  188.       return (FALSE);
  189.      if (!xdr_int(xdrs, &objp->rev))
  190.       return (FALSE);
  191.      
  192.      if (xdrs->x_op == XDR_FREE) 
  193.       i = objp->memAlloc*objp->ndim;
  194.      else
  195.       i = objp->ndata*objp->ndim;
  196.      if (!xdr_floatarray(xdrs, (char **)&objp->data, &i, UINT_MAX))
  197.       return (FALSE);
  198.  
  199.  
  200.      if (xdrs->x_op == XDR_DECODE)
  201.      {
  202.       objp->ndata = i/objp->ndim;
  203.       objp->memAlloc = objp->ndata;
  204.      }
  205.  
  206.      if (!xdr_int(xdrs, &objp->extremeBad)) {
  207.       return (FALSE);
  208.      }
  209.      if (!xdr_floatarray(xdrs, (char **)&objp->nlow, (u_int *)&objp->ndim,
  210.              UINT_MAX))
  211.       return (FALSE);
  212.      if (!xdr_floatarray(xdrs, (char **)&objp->nhigh, (u_int *)&objp->ndim,
  213.              UINT_MAX))
  214.       return (FALSE);
  215.  
  216.      if (!xdr_mywrapstring(xdrs, (char **)&objp->title )) {
  217.       return (FALSE);
  218.      }
  219.  
  220.      if (xdrs->x_op == XDR_DECODE)
  221.      {
  222.       if ( (objp->label = (char **)malloc(objp->ndim * sizeof(char *)))
  223.           == NULL) 
  224.       {
  225.            return (FALSE);
  226.       }
  227.      }
  228.      for (i=0; i<objp->ndim; i++) 
  229.      {
  230.       if (xdrs->x_op == XDR_DECODE) objp->label[i] = NULL;
  231.            if (!xdr_mywrapstring(xdrs, (char **)&objp->label[i] )) {
  232.            return (FALSE);
  233.       }
  234.      }
  235.      if (xdrs->x_op == XDR_FREE)
  236.       free(objp->label);
  237.       
  238.      return (TRUE);
  239. }
  240.  
  241.  
  242.  
  243.  
  244. bool_t xdr_ntuple(XDR *xdrs, ntuple *objp)
  245. {
  246.      if (!xdr_pointer(xdrs, (char **)objp, sizeof(ntuple_t), xdr_ntuple_t))
  247.      {
  248.       return (FALSE);
  249.      }
  250.      return (TRUE);
  251. }
  252.  
  253.  
  254. bool_t xdr_bins_t(XDR *xdrs, bins_t *objp)
  255. {
  256.      void *p;
  257.      unsigned int c;
  258.      int i,j;
  259.      int oldTot[3][3];        /* for reading pre 3.8 version */
  260.      
  261.      p = (void *) &objp->flags;
  262.      c = sizeof(objp->flags);
  263.      if (!xdr_bytes(xdrs, &p, &c, c))
  264.      {
  265.       return (FALSE);
  266.      }
  267.  
  268.      /*
  269.       * xAxis data
  270.       */
  271.      if (!xdr_int(xdrs, &objp->xAxis.nBins)) {
  272.       return (FALSE);
  273.      }
  274.      if (!xdr_float(xdrs, &objp->xAxis.low)) {
  275.       return (FALSE);
  276.      }
  277.      if (!xdr_float(xdrs, &objp->xAxis.high)) {
  278.       return (FALSE);
  279.      }
  280.      if (!xdr_vector(xdrs, (char *)objp->xAxis.moments, 3, 
  281.              sizeof(float), xdr_float)) 
  282.      {
  283.       return (FALSE);
  284.      }
  285.  
  286.      /*
  287.       * yAxis data
  288.       */
  289.      if (!xdr_int(xdrs, &objp->yAxis.nBins))
  290.       return (FALSE);
  291.      if (!xdr_float(xdrs, &objp->yAxis.low))
  292.       return (FALSE);
  293.      if (!xdr_float(xdrs, &objp->yAxis.high))
  294.       return (FALSE);
  295.      if (!xdr_vector(xdrs, (char *)objp->yAxis.moments, 3,
  296.              sizeof(float), xdr_float)) 
  297.       return (FALSE);
  298.  
  299.      if (!xdr_int(xdrs, &objp->binAlloc))
  300.       return (FALSE);
  301.      if (!xdr_int(xdrs, &objp->ndata))
  302.       return (FALSE);
  303.      if (objp->flags.fixed || xdrs->x_op == XDR_FREE)
  304.      {
  305.       if (!xdr_floatarray(xdrs, (char **)&objp->data,
  306.                   (u_int *)&objp->binAlloc, UINT_MAX))
  307.            return (FALSE);
  308.       if (!xdr_floatarray(xdrs, (char **)&objp->variance,
  309.                   (u_int *)&objp->binAlloc, UINT_MAX))
  310.            return (FALSE);
  311.      }
  312.      else if (xdrs->x_op == XDR_DECODE)
  313.      {
  314.       objp->ndata = 0;
  315.       objp->data = NULL;
  316.       objp->variance = NULL;
  317.       objp->binAlloc = 0;
  318.      }
  319.      
  320.      if (fileVersion < 3.8001)    /* add fuzz for float compare */
  321.      {
  322.       if (!xdr_vector(xdrs, (char *)oldTot,
  323.               9, sizeof(int), xdr_int)) 
  324.            return (FALSE);
  325.       for (i=0; i<3; i++)
  326.            for (j=0; j<3; j++)
  327.             objp->totals[i][j] = oldTot[i][j];
  328.      }
  329.      else
  330.      {
  331.       if (!xdr_vector(xdrs, (char *)objp->totals,
  332.               9, sizeof(int), xdr_float)) 
  333.            return (FALSE);
  334.      }
  335.  
  336.      if (!xdr_float(xdrs, &objp->binMin))
  337.       return (FALSE);
  338.      if (!xdr_float(xdrs, &objp->binMax))
  339.       return (FALSE);
  340.  
  341.      return (TRUE);
  342. }
  343.  
  344.  
  345. bool_t xdr_axis_t(XDR *xdrs, axis_t *objp)
  346. {
  347.      void *p;
  348.      unsigned int c;
  349.      
  350.      if (!xdr_float(xdrs, &objp->low)) {
  351.       return (FALSE);
  352.      }
  353.      if (!xdr_float(xdrs, &objp->high)) {
  354.       return (FALSE);
  355.      }
  356.      if (!xdr_mywrapstring(xdrs, (char **)&objp->label )) {
  357.       return (FALSE);
  358.      }
  359.  
  360.      p = (void *) &objp->flags;
  361.      c = sizeof(objp->flags);
  362.      if (!xdr_bytes(xdrs, &p, &c, c))
  363.      {
  364.       return (FALSE);
  365.      }
  366.  
  367.      if (!xdr_float(xdrs, &objp->tickLength)) {
  368.       return (FALSE);
  369.      }
  370.      if (!xdr_float(xdrs, &objp->scaleFontSize)) {
  371.       return (FALSE);
  372.      }
  373.      if (!xdr_float(xdrs, &objp->firstTick)) {
  374.       return (FALSE);
  375.      }
  376.      if (!xdr_float(xdrs, &objp->tickStep)) {
  377.       return (FALSE);
  378.      }
  379.      if (!xdr_int(xdrs, &objp->numMinorTicks)) {
  380.       return (FALSE);
  381.      }
  382.      
  383.      return (TRUE);
  384. }
  385.  
  386.  
  387. bool_t xdr_bind_strt_t(XDR *xdrs, bind_strt_t *objp)
  388. {
  389.      if (!xdr_int(xdrs, &objp->x)) {
  390.       return (FALSE);
  391.      }
  392.      if (!xdr_int(xdrs, &objp->y)) {
  393.       return (FALSE);
  394.      }
  395.      if (!xdr_int(xdrs, &objp->z)) {
  396.       return (FALSE);
  397.      }
  398.      if (!xdr_int(xdrs, &objp->weight)) {
  399.       return (FALSE);
  400.      }
  401.      if (!xdr_int(xdrs, &objp->xerror)) {
  402.       return (FALSE);
  403.      }
  404.      if (!xdr_int(xdrs, &objp->yerror)) {
  405.       return (FALSE);
  406.      }
  407.      return (TRUE);
  408. }
  409.  
  410.  
  411. bool_t xdr_display_t(XDR *xdrs, display_t *objp)
  412. {
  413.      void *p;
  414.      unsigned int c;
  415.      
  416.      /*
  417.       * write the ntuple pointer as an integer. Integer is index into 
  418.       * the list of ntuple in a record.
  419.       */
  420.      if (!xdr_int(xdrs, (int *)&objp->ntuple)) {
  421.       return (FALSE);
  422.      }
  423.      if (!xdr_int(xdrs, &objp->nt_rev)) {
  424.       return (FALSE);
  425.      }
  426.      if (!xdr_mywrapstring(xdrs, (char **)&objp->ntFile ))
  427.      {
  428.       return (FALSE);
  429.      }
  430.  
  431.      if (!xdr_int(xdrs, &objp->dim)) {
  432.       return (FALSE);
  433.      }
  434.      if (!xdr_enum(xdrs, (int *)&objp->graphtype)) {
  435.       return (FALSE);
  436.      }
  437.      if (!xdr_enum(xdrs, (int *)&objp->drawtype)) {
  438.       return (FALSE);
  439.      }
  440.  
  441.      p = (void *) &objp->flags;
  442.      c = sizeof(objp->flags);
  443.      if (!xdr_bytes(xdrs, &p, &c, c))
  444.      {
  445.       return (FALSE);
  446.      }
  447.  
  448.      if (!xdr_mywrapstring(xdrs, (char **)&objp->title ))
  449.      {
  450.       return (FALSE);
  451.      }
  452.  
  453.  
  454.      if (!xdr_bins_t(xdrs, &objp->bins))
  455.      {
  456.       return (FALSE);
  457.      }
  458.      
  459.      if (!xdr_axis_t(xdrs, &objp->xAxis)) {
  460.       return (FALSE);
  461.      }
  462.      if (!xdr_axis_t(xdrs, &objp->yAxis)) {
  463.       return (FALSE);
  464.      }
  465.      if (!xdr_axis_t(xdrs, &objp->zAxis)) {
  466.       return (FALSE);
  467.      }
  468.  
  469.      if (!xdr_enum(xdrs, (int *)&objp->plotSymbol)) {
  470.       return (FALSE);
  471.      }
  472.      if (!xdr_float(xdrs, &objp->symbolSize)) {
  473.       return (FALSE);
  474.      }
  475.      if (!xdr_enum(xdrs, (int *)&objp->lineStyle)) {
  476.       return (FALSE);
  477.      }
  478.      if (!xdr_bind_strt_t(xdrs, &objp->binding)) {
  479.       return (FALSE);
  480.      }
  481.      if (!xdr_rectangle(xdrs, &objp->drawRect)) {
  482.       return (FALSE);
  483.      }
  484.      if (!xdr_rectangle(xdrs, &objp->marginRect)) {
  485.       return (FALSE);
  486.      }
  487.      if (!xdr_func_id(xdrs, &objp->nt_cut)) {
  488.       return (FALSE);
  489.      }
  490.      if (!xdr_func_id(xdrs, &objp->bin_func)) {
  491.       return (FALSE);
  492.      }
  493.      if (!xdr_func_id(xdrs, &objp->plot_func)) {
  494.       return (FALSE);
  495.      }
  496.      if (!xdr_func_id(xdrs, &objp->binToColor)) {
  497.       return (FALSE);
  498.      }
  499.  
  500.      return (TRUE);
  501. }
  502.  
  503.  
  504.  
  505.  
  506. bool_t xdr_display(XDR *xdrs, display *objp)
  507. {
  508.      if (!xdr_pointer(xdrs, (char **)objp, sizeof(display_t), 
  509.               xdr_display_t)) {
  510.       return (FALSE);
  511.      }
  512.      return (TRUE);
  513. }
  514.  
  515.  
  516.  
  517. bool_t xdr_hippo_rec(XDR *xdrs, hippo_rec *objp)
  518. {
  519.      char *str = STRUCT_VERSION;
  520.      int l,i;
  521.      char msg[80];
  522.      
  523.      /*
  524.       * first check that the magic number and structure version
  525.       * are the same as I was compiled with.
  526.       */
  527.      if (strcmp(objp->magic, MAGIC) != 0)
  528.       return (FALSE);
  529.      if (strcmp(objp->s_version, STRUCT_VERSION) != 0)
  530.       return (FALSE);
  531.      
  532.      /*
  533.       * read/write the magic number. Pad (with NULL) or truncate to 4 bytes.
  534.       */
  535.      strncpy(msg,MAGIC,4);
  536.      l = strlen(MAGIC);
  537.      if (l>4) l=4;
  538.      switch (xdrs->x_op)
  539.      {
  540.      case XDR_FREE:
  541.       free(objp->magic);
  542.       break;
  543.       
  544.      case XDR_DECODE:        /* ie. read */
  545.       if (! XDR_GETBYTES(xdrs,msg,4*sizeof(char)))
  546.            return FALSE;
  547.       break;
  548.       
  549.      case XDR_ENCODE:
  550.       if (! XDR_PUTBYTES(xdrs,msg,4*sizeof(char)))
  551.            return FALSE;
  552.       break;
  553.      }      
  554.      if (xdrs->x_op == XDR_DECODE && strncmp(msg,MAGIC,l) != 0)
  555.       return (FALSE);
  556.  
  557.      /*
  558.       * read/write the structure version.
  559.       *
  560.       */
  561.      if (xdrs->x_op != XDR_DECODE)
  562.       str = objp->s_version;
  563.      else 
  564.       str = NULL;
  565.      if (!xdr_mywrapstring(xdrs, (char **)&str ))
  566.       return FALSE;
  567.  
  568.      /*
  569.       * check the version number.
  570.       */
  571.      if (xdrs->x_op == XDR_DECODE)
  572.      {
  573.       i = sscanf(str,"%*s %*s %f",&fileVersion);
  574.       if (i < 1 || fileVersion < 3.5)
  575.       {
  576.            sprintf(msg,"Old hippo file version: %s",str);
  577.            h_error(msg);
  578.            h_error("No displays will be read");
  579.       }
  580.       free(str);
  581.      }
  582.      else
  583.       fileVersion = CURRENT_VER_NUM;
  584.      
  585.      
  586.      if (!xdr_array(xdrs, (char **) &objp->nt_list,
  587.             (u_int *)&objp->num_nt, UINT_MAX,
  588.             sizeof(ntuple), xdr_ntuple ))
  589.      {
  590.       return (FALSE);
  591.      }
  592.  
  593.      if (fileVersion < 3.5001)    /* add fuzz for float compare */
  594.       objp->num_disp = 0;
  595.      else
  596.       if (!xdr_array(xdrs, (char **)&objp->disp_list,
  597.              (u_int *)&objp->num_disp, UINT_MAX,
  598.              sizeof(display), xdr_display ))
  599.       {
  600.            return (FALSE);
  601.       }
  602.      
  603.      return (TRUE);
  604. }
  605.